package com.my.screenadapter.mdir;

import android.os.Environment;
import android.text.TextUtils;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;

/**
 * 参照 FaceControl
 * 2018-7-4 15:52:46
 */
public class CopyFileUtils {
    public static Logger logger = Logger.getLogger(CopyFileUtils.class);
    //广告保存地址
    public static final String AD_FILE_PATH = Environment.getExternalStorageDirectory() + "/oids_rec/adFile/";  //storage/emulated/0/oids_rec/adFile
    //默认广告名字
    public static final String DEFAULT_VIDEO_NAME="defaultVideo.mp4";

    public static String getNewFileName(){
        String defaultPath = CopyFileUtils.AD_FILE_PATH ;
        File dir = new File(defaultPath);
        if (!dir.exists()||!dir.isDirectory()){
            dir.mkdirs();
        }
        Random random = new Random();
        String fStr = CopyFileUtils.AD_FILE_PATH + "defaultVideo" + random.nextInt(10000000) + ".mp4";
        File file = new File(fStr);
        if (!file.exists()){
            return fStr;
        }else {
            return getNewFileName();
        }
    };

    public static void copyFile(String source, String dest) throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            input.close();
            output.close();
        }
    }




}
